package com.adam.springbucks.controller;

import com.adam.springbucks.per.entity.Person;
import com.adam.springbucks.service.PersonService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: PersonController
 * @Author Adam
 * @Date 2022/10/29
 * @Description:    接口缓存案例
 * 访问 http://localhost:8080/person
 */
@RestController
public class PersonController {

    @Resource
    private PersonService personService;

    @GetMapping("/person")
    public Person getPerson(Long id) {
        return personService.findPerson(id);
    }

    /**
     * 根据主键批量查询
     * @param ids
     * @return
     */
    @PostMapping("/findPersonByIds")
    public List<Person> findPersonByIds(List<Long> ids) {
        return personService.findPersonByIds(ids);
    }


    @PutMapping("/person")
    public Person setPerson(Person person) {
        return personService.setPerson(person);
    }

    @DeleteMapping("/person")
    public String deletePerson(Long id) {
        return personService.deletePerson(id) ? "删除成功" : "删除失败";
    }

    @PostMapping("/person")
    public Person addPerson(Person person) {
        return personService.addPerson(person);
    }

    /**
     * 分页查询
     * @param current
     * @param size
     * @param name
     * @param age
     * @return
     */
    @PostMapping("/findPersonByNameOrAge")
    public IPage<Person> findPersonByNameOrAge(Integer current, Integer size, String name, Integer age) {
        Page<Person> personPage = new Page<>(current, size);
        return personService.findPersonByNameOrAge(personPage, name, age);
    }

    /**
     * 分页并排序查询
     * @param current
     * @param size
     * @param condition
     * @return
     */
    @PostMapping("/findAllPerson")
    public IPage<Person> findAllPerson(Integer current, Integer size, String condition) {
        Page<Person> personPage = new Page<>(current, size);
        return personService.findAllPerson(personPage, condition);
    }


}
